package com.maple.optimize.ustils;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * <p></p>
 *
 * @author maple
 */
public class CompletableFutureDemo {

    private static final List<Shop> shopList = Arrays.asList(
            new Shop("Clothing"),
            new Shop("Pants"),
            new Shop("ShortSleeve1"),
            new Shop("ShortSleeve2"),
            new Shop("ShortSleeve3"),
            new Shop("Sweater")
    );

    private static List<String> findPriceSync(String product) {
//        List<String> result = new ArrayList<>();
//        for (Shop shop : shopList) {
//            result.add(String.format("%s price is %.2f", shop.getName(), shop.getPrice(product)));
//        }
        return shopList.stream().map(shop -> String.format("%s price is %.2f", shop.getName(), shop.getPrice(product))).collect(Collectors.toList());
    }

    private static List<String> findPriceAsync(String product) {
        List<String> result = new ArrayList<>();

        // 方式1：runAsync 无返回值
        List<CompletableFuture<?>> futuresTest = new ArrayList<>();
        shopList.forEach(shop -> {
            futuresTest.add(CompletableFuture.runAsync(() -> {
                try {
                    result.add(String.format("%s price is %.2f", shop.getName(), shop.getPrice(product)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }));
        });
        CompletableFuture.allOf(futuresTest.toArray(new CompletableFuture[0])).join();
        return result;

//        方式2：supplyAsync 有返回值
//        List<CompletableFuture<String>> futures = new ArrayList<>();
//        shopList.forEach(shop -> {
//            futures.add(CompletableFuture.supplyAsync(() -> {
//                try {
//                   return String.format("%s price is %.2f", shop.getName(), shop.getPrice(product));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                return null;
//            }));
//        });
//        return futures.stream().map(CompletableFuture::join).collect(Collectors.toList());

//        方式3：supplyAsync 有返回值
//        List<CompletableFuture<String>> completableFutureList = shopList.stream()
//                //转异步执行
//                .map(shop -> CompletableFuture.supplyAsync(
//                        () -> String.format("%s price is %.2f",
//                                shop.getName(), shop.getPrice(product))))
//                .collect(Collectors.toList());
//
//
//        return completableFutureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        List<String> priceSync = findPriceSync("100");
        System.out.println("first:" + (System.currentTimeMillis() - l));
        System.out.println(JSON.toJSONString(priceSync));

        l = System.currentTimeMillis();
        List<String> priceAsync = findPriceAsync("200");
        System.out.println(JSON.toJSONString(priceAsync));
        System.out.println("second:" + (System.currentTimeMillis() - l));

    }
}
